package com.wx.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wx.base.entity.PageInfo;
import com.wx.sys.dao.Wx_sys_role_DAO;
import com.wx.sys.entity.Wx_sys_role;
import com.wx.sys.vo.Wx_sys_role_VO;

/**
 * 角色
 * 
 * @author lenovo
 *
 */
@Transactional(readOnly = true)
@Service("Wx_sys_role_Service")
public class Wx_sys_role_Service {
	@Resource(name = "Wx_sys_role_DAO")
	private Wx_sys_role_DAO wx_sys_role_DAO;

	/**
	 * 分页查询
	 */
	public PageInfo selectPage(Wx_sys_role_VO wx_sys_role_VO) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PSIZE", wx_sys_role_VO.getRows());
		map.put("BEGIN", (wx_sys_role_VO.getPage() - 1) * wx_sys_role_VO.getRows());
		Integer count = this.wx_sys_role_DAO.selectPageCount(map);
		if (count == 0) {
			return new PageInfo(wx_sys_role_VO.getRows(), wx_sys_role_VO.getPage(), count, new ArrayList<Wx_sys_role>());
		}
		List<Wx_sys_role> list = this.wx_sys_role_DAO.selectPage(map);
		PageInfo pageInfo = new PageInfo(wx_sys_role_VO.getRows(), wx_sys_role_VO.getPage(), count, list);
		return pageInfo;
	}

	/**
	 * 查询一条记录
	 */
	public Wx_sys_role get(java.lang.Long id) throws Exception {
		Wx_sys_role Wx_sys_role = wx_sys_role_DAO.getById(id);
		return Wx_sys_role;
	}

	/**
	 * 新增
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean add(Wx_sys_role Wx_sys_role) throws Exception {
		Integer result = wx_sys_role_DAO.add(Wx_sys_role);
		return result > 0 ? true : false;
	}

	/**
	 * 修改
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean update(Wx_sys_role wx_sys_role) throws Exception {
		Integer result = wx_sys_role_DAO.update(wx_sys_role);
		return result > 0 ? true : false;
	}

	/**
	 * 删除
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean delete(java.lang.Long id) throws Exception {
		Integer result = wx_sys_role_DAO.deleteById(id);
		return result > 0 ? true : false;
	}

	/**
	 * 获取所有数据
	 */
	public List<Wx_sys_role> selectAll(Wx_sys_role_VO Wx_sys_role_VO) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (Wx_sys_role_VO != null) {
			if (StringUtils.isNotEmpty(Wx_sys_role_VO.getRoleName())) {
				map.put("roleName", Wx_sys_role_VO.getRoleName());
			}
		}
		return wx_sys_role_DAO.selectAll(map);
	}
}
